﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Universidad.Negocio.DomainModel;
using Universidad.DataAccess.DataMapper;
using UniversidadWeb.Models;

namespace UniversidadWeb.Controllers
{
    public class AlumnosController : Controller
    {
        private UniversidadContext db;
        private AlumnosRepo alumnosRepo;

        public AlumnosController()
        {
            this.db = new UniversidadContext();
            this.alumnosRepo = new AlumnosRepo(db);
        }

        //
        // GET: /Alumnos/

        public ActionResult Index()
        {
            return View(this.alumnosRepo.Todos());
        }

        //
        // GET: /Alumnos/Details/5

        public ActionResult Details(int id = 0)
        {
            Alumno alumno = this.alumnosRepo.Obtener(id);
            if (alumno == null)
            {
                return HttpNotFound();
            }
            return View(new AlumnoVM(alumno));
        }

        //
        // GET: /Alumnos/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Alumnos/Create

        [HttpPost]
        public ActionResult Create(Alumno alumno)
        {
            if (ModelState.IsValid)
            {
                this.alumnosRepo.Agregar(alumno);
                return RedirectToAction("Index");
            }

            return View(alumno);
        }

        //
        // GET: /Alumnos/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Alumno alumno = this.alumnosRepo.Obtener(id);
            if (alumno == null)
            {
                return HttpNotFound();
            }
            return View(alumno);
        }

        //
        // POST: /Alumnos/Edit/5

        [HttpPost]
        public ActionResult Edit(Alumno alumnoModificado)
        {
            if (ModelState.IsValid)
            {
                Alumno alumnoActual = this.alumnosRepo.Obtener(alumnoModificado.Id);
                alumnoActual.Nombre = alumnoModificado.Nombre;
                alumnoActual.Email = alumnoModificado.Email;
                this.alumnosRepo.Modificar(alumnoActual);
                return RedirectToAction("Index");
            }
            return View(alumnoModificado);
        }

        //
        // GET: /Alumnos/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Alumno alumno = this.alumnosRepo.Obtener(id);
            if (alumno == null)
            {
                return HttpNotFound();
            }
            return View(alumno);
        }

        //
        // POST: /Alumnos/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            this.alumnosRepo.Eliminar(id);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}